import numpy as np
from shapely.geometry import LineString, Point, Polygon, LinearRing
from shapely.ops import split
try:
    from foot_of_perpendicular import foot_of_perpendicular
    from translate_line import translate_line
except:
    from .foot_of_perpendicular import foot_of_perpendicular
    from .translate_line import translate_line

def scan_polygon_with_intervals(local_end_boundary_poly, A, B, L, tol=1e-8):

    if not isinstance(local_end_boundary_poly, Polygon):
        raise TypeError("输入必须是一个shapely.Polygon对象")
    if L <= tol:
        raise ValueError("扫描间隔L必须大于0")
    if np.linalg.norm(np.array(B)-np.array(A)) < tol:
        raise ValueError("A和B不能是同一个点")

    # AB_line = LineString([A, B])

    # 得到多边形边界所有点到直线AB的垂点坐标
    perpendicular_direction_and_length_list = [foot_of_perpendicular(A, B, [lon,lat]) for lon,lat in local_end_boundary_poly.exterior.coords]
    # 找到最大距离对应的点
    max_direction = max(perpendicular_direction_and_length_list, key=lambda x: x[1])[0]
    max_length = np.linalg.norm(max_direction)
    #max_unit_direction = max_direction/max_length
    # 找到最小距离对应的点
    min_direction = min(perpendicular_direction_and_length_list, key=lambda x: x[1])[0]
    min_length = np.linalg.norm(min_direction)
    min_unit_direction = min_direction/min_length

    A = np.array(A)
    B = np.array(B)
    u = min_unit_direction
    # 计算平移向量
    v = u * (min_length+L)

    # 平移点A和B
    A = A + v
    B = B + v

    intersection_groups = []

    moved_length = 0
    while moved_length <= max_length:
        Moved_line = translate_line(A, B, min_unit_direction, moved_length)

        local_end_boundary = local_end_boundary_poly.boundary
        intersection = Moved_line.intersection(local_end_boundary)
        # 处理交点结果
        current_intersections = []
        if not intersection.is_empty:
            if intersection.geom_type == 'MultiPoint' and not intersection.geom_type == 'LineString':
                for point in intersection.geoms:
                    current_intersections.append([point.x, point.y])

        # 将当前扫描位置的交点组添加到结果
        intersection_groups.append(current_intersections)
        # 移动到下一个位置
        moved_length = moved_length + L

    return intersection_groups






def scan_polygon_with_intervals_path(local_end_boundary_poly, A, B, L, tol=1e-8):

    if not isinstance(local_end_boundary_poly, Polygon):
        raise TypeError("输入必须是一个shapely.Polygon对象")
    if L <= tol:
        raise ValueError("扫描间隔L必须大于0")
    if np.linalg.norm(np.array(B)-np.array(A)) < tol:
        raise ValueError("A和B不能是同一个点")

    # AB_line = LineString([A, B])

    # 得到多边形边界所有点到直线AB的垂点坐标
    perpendicular_direction_and_length_list = [foot_of_perpendicular(A, B, [lon,lat]) for lon,lat in local_end_boundary_poly.exterior.coords]
    # 找到最大距离对应的点
    max_direction = max(perpendicular_direction_and_length_list, key=lambda x: x[1])[0]
    max_length = np.linalg.norm(max_direction)
    #max_unit_direction = max_direction/max_length
    # 找到最小距离对应的点
    min_direction = min(perpendicular_direction_and_length_list, key=lambda x: x[1])[0]
    min_length = np.linalg.norm(min_direction)
    min_unit_direction = min_direction/min_length

    intersection_groups = []

    moved_length = 0
    while moved_length <= max_length:
        Moved_line = translate_line(A, B, min_unit_direction, moved_length)

        local_end_boundary = local_end_boundary_poly.boundary
        intersection = Moved_line.intersection(local_end_boundary)
        # 处理交点结果
        current_intersections = []
        if not intersection.is_empty:
            if intersection.geom_type == 'MultiPoint' and not intersection.geom_type == 'LineString':
                for point in intersection.geoms:
                    current_intersections.append([point.x, point.y])

        # 将当前扫描位置的交点组添加到结果
        intersection_groups.append(current_intersections)
        # 移动到下一个位置
        moved_length = moved_length + L


    return intersection_groups




